home *** CD-ROM | disk | FTP | other *** search
/ Delphi Programmer's Power Pack / Delphi Volume 1.iso / e_to_l / fbuilder / delphi / demos / rttifm.pas < prev    next >
Pascal/Delphi Source File  |  1996-09-15  |  6KB  |  220 lines

  1. { FormulaBuilder                }
  2. { YGB Software, Inc.            }
  3. { Copyright 1995 Clayton Collie }
  4. { All rights reserved           }
  5.  
  6. {* RTTI Demo Form. Demonstrates the use of the TRTTIExpression class *}
  7.  
  8. unit Rttifm;
  9. interface
  10. uses
  11.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  12.   Forms, Dialogs, Grids, Outline, StdCtrls, Buttons, ExtCtrls,FBRTComp, DB,
  13.   DBGrids, DBTables,FB_Rtti;
  14.  
  15. type
  16.   TRTTIDemoForm = class(TForm)
  17.     olObjects: TOutline;
  18.     BitBtn1: TBitBtn;
  19.     CustomerTable: TTable;
  20.     dbgCompany: TDBGrid;
  21.     CustomerSource: TDataSource;
  22.     CustomerTableCustNo: TFloatField;
  23.     CustomerTableCompany: TStringField;
  24.     CustomerTableAddr1: TStringField;
  25.     CustomerTableAddr2: TStringField;
  26.     CustomerTableCity: TStringField;
  27.     CustomerTableState: TStringField;
  28.     CustomerTableZip: TStringField;
  29.     CustomerTableCountry: TStringField;
  30.     CustomerTablePhone: TStringField;
  31.     CustomerTableFAX: TStringField;
  32.     CustomerTableTaxRate: TFloatField;
  33.     CustomerTableContact: TStringField;
  34.     CustomerTableLastInvoiceDate: TDateTimeField;
  35.     gbxProperty: TGroupBox;
  36.     PropNamePanel: TPanel;
  37.     TypenamePanel: TPanel;
  38.     PropValuePanel: TPanel;
  39.     PropvalueEdit: TEdit;
  40.     btnChangeProp: TSpeedButton;
  41.     gbxExpression: TGroupBox;
  42.     moRTTIExpression: TMemo;
  43.     TestButton: TBitBtn;
  44.     gbxResult: TGroupBox;
  45.     edResult: TEdit;
  46.     lblObjects: TLabel;
  47.     procedure FormCreate(Sender: TObject);
  48.     procedure TestButtonClick(Sender: TObject);
  49.     procedure FormDestroy(Sender: TObject);
  50.     procedure olObjectsClick(Sender: TObject);
  51.     procedure BtnChangePropClick(Sender: TObject);
  52.     procedure olObjectsDblClick(Sender: TObject);
  53.   private
  54.     { Private declarations }
  55.     Expr : TRTTIExpression;
  56.     Function GetCurrentProp  : TInstanceProperty;
  57.     Function GetCurrPropPath : String;
  58.     Procedure LoadPropertyList;
  59.   public
  60.     { Public declarations }
  61.     Procedure TestExpr;
  62.   end;
  63.  
  64. var
  65.   RTTIDemoForm: TRTTIDemoForm;
  66.  
  67. Procedure ShowRTTIDemo;
  68.  
  69. implementation
  70. uses fbcalc,typinfo;
  71. {$R *.DFM}
  72.  
  73.  
  74. Procedure ShowRTTIDemo;
  75. var dlg : TRTTIDemoForm;
  76. begin
  77.   dlg := TRTTIDemoForm.Create(Application);
  78.   TRY
  79.     Dlg.ShowModal;
  80.   FINALLY
  81.     Dlg.Free;
  82.   END;
  83. end;
  84.  
  85.  
  86. Function RemoveSpaces(const s : String):string;
  87. var i : integer;
  88. begin
  89.   result := '';
  90.   for i := 1 to length(s) do begin
  91.       if not(s[i] in [#32,#160]) then
  92.          result := result + s[i];
  93.   end;
  94. end;
  95.  
  96. procedure TRTTIDemoForm.FormCreate(Sender: TObject);
  97. begin
  98.   Application.Name := 'App';
  99.   Name      := 'RTTIForm';
  100.   Expr      := TRTTIExpression.Create(Self);
  101.   Expr.Root := Application;
  102.   LoadPropertyList;
  103. end;
  104.  
  105.  
  106. Procedure TRTTIDemoForm.LoadPropertyList;
  107. var lines : TStringList;
  108. begin
  109.   olObjects.BeginUpdate;
  110.   lines := TStringList.Create;
  111.   { The following operation takes a bit of time, so change cursor to }
  112.   { hourglass                                                        }
  113.   Cursor := crHourGlass;
  114.   TRY
  115.     GetComponentProperties({Self}Application,tkProperties {+ tkMethods},Lines,0);
  116.   FINALLY
  117.     Cursor := crDefault;
  118.   END;
  119.   olObjects.Lines.Assign(lines);
  120.   lines.free;
  121.   olObjects.EndUpdate;
  122. end;
  123.  
  124. Procedure TRTTIDemoForm.TestExpr;
  125. var i : integer;
  126.     res : string;
  127. begin
  128.   Expr.Status  := EXPR_SUCCESS;
  129.   Expr.Lines   := moRTTIExpression.Lines;
  130.   if Expr.Status <> EXPR_SUCCESS then
  131.      edResult.Text := ' Error : '+Expr.StatusText
  132.     else
  133.      edResult.Text := Expr.AsString;
  134.  end;
  135.  
  136. procedure TRTTIDemoForm.TestButtonClick(Sender: TObject);
  137. begin
  138.   TestExpr;
  139. end;
  140.  
  141. procedure TRTTIDemoForm.FormDestroy(Sender: TObject);
  142. begin
  143.  { Kill the data collected by the GetComponentProperties call }
  144.   FreePropertyData(olObjects.Lines);
  145. end;
  146.  
  147. Function TRTTIDemoForm.GetCurrentProp : TInstanceProperty;
  148. var node : TOutlineNode;
  149. begin
  150.   node := olObjects.Items[olObjects.SelectedItem];
  151.   result := TInstanceProperty(node.data);
  152. end;
  153.  
  154.  
  155. {* Display the current instance property, along with its *}
  156. {* Type and current value, which is editable             *}
  157. procedure TRTTIDemoForm.olObjectsClick(Sender: TObject);
  158. var node : TOutLineNode;
  159.     prop : TInstanceProperty;
  160.     tempstr : string;
  161. begin
  162.   node := olObjects.Items[olObjects.SelectedItem];
  163.   prop := TInstanceProperty(node.Data);
  164.   PropNamePanel.Caption  := ' '+GetCurrPropPath;
  165.   if Assigned(prop) then
  166.   begin
  167.     TypenamePanel.Caption  := prop.Typename;
  168.     PropValueEdit.Text     := prop.AsString;
  169.     PropValueEdit.Enabled  := True;
  170.   end
  171.  else
  172.   begin
  173.     TypenamePanel.Caption := '';
  174.     PropValueEdit.Text    := '';
  175.     PropValueEdit.Enabled := False;
  176.   end;
  177. end;
  178.  
  179.  
  180. {* Change the value of a property based on user input. If successful *}
  181. {* it will have the expected runtime effect as defined by the object *}
  182. {* designer                                                          *}
  183. procedure TRTTIDemoForm.BtnChangePropClick(Sender: TObject);
  184. var prop : TInstanceProperty;
  185. begin
  186.   prop := getCurrentProp;
  187.   if Assigned(prop) then
  188.   begin
  189.     { Setting an object's property is this easy !}
  190.     Prop.AsString := PropValueEdit.Text;
  191.   end;
  192. end;
  193.  
  194. {* Returns the "Dot-notated" path of the current property *}
  195. {* Note that the Root object's name is implicit, so it is *}
  196. {* removed from the path                                  *}
  197. Function TRTTIDemoForm.getCurrPropPath : string;
  198. var node : TOutlineNode;
  199.     p    : integer;
  200. begin
  201.   node := olObjects.Items[olObjects.SelectedItem];
  202.   result := RemoveSpaces(node.fullpath);
  203.   p := pos('.',result);
  204.   if (p > 0) then
  205.      system.delete(result,1,p);
  206. end;
  207.  
  208.  
  209. {* Put the current property in the expression edit Window *}
  210.  
  211. procedure TRTTIDemoForm.olObjectsDblClick(Sender: TObject);
  212. var prop : TInstanceProperty;
  213. begin
  214.   prop := getCurrentProp;
  215.   if assigned(prop) and (Prop.Kind <> tkClass) then
  216.      moRTTIExpression.SelText := '['+ GetCurrPropPath+']';
  217. end;
  218.  
  219. end.
  220.